package com.supreme.commonmybatis.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Date;

@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
//        this.strictInsertFill(metaObject, "createdAt", LocalDateTime.class, LocalDateTime.now());
//        this.strictInsertFill(metaObject, "updatedAt", LocalDateTime.class, LocalDateTime.now());

//        final Object createdAt = getFieldValByName("createdAt", metaObject);
//        if (createdAt == null) {
//            setFieldValByName("createdAt", new Date(), metaObject);
//        }
//
//        final Object updatedAt = getFieldValByName("updatedAt", metaObject);
//        if (updatedAt == null) {
//            setFieldValByName("updatedAt", new Date(), metaObject);
//        }

        setFieldValByName("createdAt", new Date(), metaObject);
        setFieldValByName("updatedAt", new Date(), metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
//        this.strictUpdateFill(metaObject, "updatedAt", LocalDateTime.class, LocalDateTime.now());

//        final Object updatedAt = getFieldValByName("updatedAt", metaObject);
//        if (updatedAt == null) {
//            setFieldValByName("updatedAt", new Date(), metaObject);
//        }

        setFieldValByName("updatedAt", new Date(), metaObject);
    }
}
